#include "Seeding.h"

#include <seqan/index.h>

using namespace std;
using namespace seqan;

Seeding::Seeding(int seed1)
{
    //ctor
    seed = seed1;
}

Seeding::~Seeding()
{
    //dtor
}

void Seeding::get_matches(vector<Match> &matches, vector<Sequence> &short1, vector<Sequence> &long1)
{
    for (int k = 0; k < long1.size(); k++)
    {
        Index<CharString> index(long1[k].get_seq());
        Finder<Index<CharString> > finder(index);
        for (int i = 0; i < short1.size(); i++)
        {
            for (int j = 0; j < short1[i].get_seq().length()-seed; j++)
            {
                //foward strand
//                cout << short1[i].get_seq().length() << '\t' << j+seed << endl;
                while (find(finder, short1[i].get_seq().substr(j, seed)))
                {
                    matches.push_back(Match(&short1[i], &long1[k], int(j), int(beginPosition(finder)), false));
                }
                clear(finder);
                //reverse complement
                while (find(finder, RC(short1[i].get_seq().substr(j, seed))))
                {
                    matches.push_back(Match(&short1[i], &long1[k], int(j), int(beginPosition(finder)), true));
                }
                clear(finder);
            }
        }
    }
}

//makes reverse complment of a sequence
string Seeding::RC(string seq)
{
    string rc = seq;
    int length = seq.length();
    for (int i = 0; i<length; i++)
    {
        switch (rc[i])
        {
            case 'A':
                rc[i]='T';
                break;
            case 'T':
                rc[i]='A';
                break;
            case 'C':
                rc[i]='G';
                break;
            case 'G':
                rc[i]='C';
                break;
        }
    }
    reverse(rc.begin(), rc.end());
    return rc;
}
